<?php

use yii\db\Migration;

class m181002_122631_card_add_term_info extends Migration
{
    public function safeUp()
    {
        $this->addColumn('{{%card}}', 'term_length', $this->integer()->notNull()->defaultValue(0)->comment('套餐有效期')->after('refresh_time'));
        $this->addColumn('{{%card}}', 'term_length_unit', $this->integer()->comment('套餐有效期单位')->after('term_length'));
        $this->addColumn('{{%card}}', 'term_end_time', $this->integer()->notNull()->defaultValue(0)->comment('套餐结束时间')->after('term_length_unit'));
        $this->createIndex('card_term_end_time_idx', '{{%card}}', ['term_end_time']);
        $this->batchInsert('{{%key_map}}', ['t', 'k', 'v'], [
            ['card_term_length_unit', 1, '分钟'],
            ['card_term_length_unit', 2, '天'],
            ['card_term_length_unit', 3, '月'],
        ]);
    }

    public function safeDown()
    {
        $this->delete('{{%key_map}}', ['t' => 'card_term_length_unit']);
        $this->dropColumn('{{%card}}', 'term_end_time');
        $this->dropColumn('{{%card}}', 'term_length_unit');
        $this->dropColumn('{{%card}}', 'term_length');
    }
}
